<?php
//validator formulare pro obnoveni hesla - jestli ten hash existuje

require_once 'Zend/Validate/Abstract.php';

class Validate_ExpireCheck extends Zend_Validate_Abstract {
    const INVALID = 'TimeOut';

    /**
     * Validation failure message template definitions
     *
     * @var array
     */
    protected $_messageTemplates = array(
        self::INVALID => "The time for change your password already expired.",
    );

    /**
     * Defined by Zend_Validate_Interface
     *
     * Returns true if and only if $value contains only hexadecimal digit characters
     *
     * @param  string $value
     * @return boolean
     */
    public function isValid($value) {
        $db = Zend_Db_Table::getDefaultAdapter();
        $select = $db->select()->from('forgot','expire')
                               ->where("hash = ?", $value);
        $expire = $db->fetchOne($select);
        $locale = 'en_GB';

        if (!$expire) {
            $this->_error(self::INVALID);
            return false;
        }

        $timedb = new Zend_Date($expire, Zend_Date::ISO_8601, $locale);
        $timenow = new Zend_Date();

        if ($timedb->getTimestamp() < $timenow->getTimestamp()) {
            $this->_error(self::INVALID);
             return false;
        }
        else {
            return true;
        }
        
    }

}
